﻿' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports DotNetNuke
Imports DotNetNuke.Entities.Modules.Actions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization

''' <summary>
''' The View class displays the content
''' 
''' Typically your view control would be used to display content or functionality in your module.
''' 
''' View may be the only control you have in your project depending on the complexity of your module
''' 
''' Because the control inherits from SiteLinksModuleBase you have access to any custom properties
''' defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
''' 
''' </summary>
Public Class View
    Inherits SiteLinksModuleBase
    Implements IActionable

    Public Function CreateSiteLink(ByRef PageToCreateLinkFor As Classes.SitePage) As String
        Try
            If IsNumeric(Trim(PageToCreateLinkFor.URL)) Then
                Dim SiteDatabase As New SiteLinksController
                Dim SourceSitePage As Classes.SitePage = SiteDatabase.GetSinglePage(CInt(PageToCreateLinkFor.URL))
                SiteDatabase = Nothing
                Dim strRenderedLink As String = "<a href='"
                strRenderedLink += SourceSitePage.TabPath.Replace("//", "/") + "'>"
                If (Settings.Contains("LinksUseTabName")) Then
                    If Settings("LinksUseTabName") = "true" Then
                        strRenderedLink += SourceSitePage.TabName
                    Else
                        If SourceSitePage.Title = "" Then
                            strRenderedLink += SourceSitePage.TabName
                        Else
                            strRenderedLink += SourceSitePage.Title
                        End If
                    End If
                Else
                    If SourceSitePage.Title = "" Then
                        strRenderedLink += SourceSitePage.TabName
                    Else
                        strRenderedLink += SourceSitePage.Title
                    End If
                End If
                strRenderedLink += "</a>"
                SourceSitePage = Nothing
                Return strRenderedLink
            Else
                Dim strRenderedLink As String = "<a href='"
                If Left(Trim(PageToCreateLinkFor.URL), 7) = "FileID=" Then
                    Dim intFileTicket As Integer = CInt(Trim(PageToCreateLinkFor.URL).Substring(7))
                    Select Case intFileTicket
                        Case Is < 1000
                            strRenderedLink += "/LinkClick.aspx?FileTicket=" + Trim(PageToCreateLinkFor.URL).Substring(7) + "'>"
                        Case Is > 1000
                            strRenderedLink += "/LinkClick.aspx?FileTicket=0" + Trim(PageToCreateLinkFor.URL).Substring(7) + "'>"
                    End Select
                ElseIf Not Trim(PageToCreateLinkFor.URL) = "" Or Not PageToCreateLinkFor.URL = Nothing Then
                    strRenderedLink += PageToCreateLinkFor.URL + "'>"
                Else
                    strRenderedLink += PageToCreateLinkFor.TabPath.Replace("//", "/") + ".aspx'>"
                End If
                If (Settings.Contains("LinksUseTabName")) Then
                    If Settings("LinksUseTabName") = "true" Then
                        strRenderedLink += PageToCreateLinkFor.TabName
                    Else
                        If PageToCreateLinkFor.Title = "" Then
                            strRenderedLink += PageToCreateLinkFor.TabName
                        Else
                            strRenderedLink += PageToCreateLinkFor.Title
                        End If
                    End If
                Else
                    If PageToCreateLinkFor.Title = "" Then
                        strRenderedLink += PageToCreateLinkFor.TabName
                    Else
                        strRenderedLink += PageToCreateLinkFor.Title
                    End If
                End If
                strRenderedLink += "</a>"
                Return strRenderedLink
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RenderOrderedListOfLinks renders an ordered list of links and adds these links to
    ''' the "SiteLinksRenderArea" panel on the view control.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub RenderOrderedListOfLinks(ByRef ListOfLinks As Classes.SitePages)
        Try
            Dim strRenderedHTML As String = ""
            If (Settings.Contains("LinksCSSClass")) Then
                strRenderedHTML += "<div class='" + HttpUtility.HtmlDecode(Settings("LinksCSSClass")) + "'><ol>"
            Else
                strRenderedHTML += "<div><ol>"
            End If
            For Each CurrentPage As Classes.SitePage In ListOfLinks
                strRenderedHTML += "<li>" + CreateSiteLink(CurrentPage) + "</li>"
            Next
            strRenderedHTML += "</ol></div>"
            Dim PlainHTMLControl As New HtmlControls.HtmlGenericControl
            PlainHTMLControl.InnerHtml = strRenderedHTML
            pnlSiteLinksRenderArea.Controls.Add(PlainHTMLControl)
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RenderUnorderedListOfLinks renders an unordered list of links and adds these links to
    ''' the "SiteLinksRenderArea" panel on the view control.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub RenderUnorderedListOfLinks(ByRef ListOfLinks As Classes.SitePages)
        Try
            Dim strRenderedHTML As String = ""
            If (Settings.Contains("LinksCSSClass")) Then
                strRenderedHTML += "<div class='" + HttpUtility.HtmlDecode(Settings("LinksCSSClass")) + "'><ul>"
            Else
                strRenderedHTML += "<div><ul>"
            End If
            For Each CurrentPage As Classes.SitePage In ListOfLinks
                strRenderedHTML += "<li>" + CreateSiteLink(CurrentPage) + "</li>"
            Next
            strRenderedHTML += "</ul></div>"
            Dim PlainHTMLControl As New HtmlControls.HtmlGenericControl
            PlainHTMLControl.InnerHtml = strRenderedHTML
            pnlSiteLinksRenderArea.Controls.Add(PlainHTMLControl)
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RenderVerticalListOfLinks renders a vertical list of links and adds these links to
    ''' the "SiteLinksRenderArea" panel on the view control.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub RenderVerticalListOfLinks(ByRef ListOfLinks As Classes.SitePages)
        Try
            Dim strRenderedHTML As String = ""
            If (Settings.Contains("LinksCSSClass")) Then
                strRenderedHTML += "<div class='" + HttpUtility.HtmlDecode(Settings("LinksCSSClass")) + "'>"
            Else
                strRenderedHTML += "<div>"
            End If
            If (Settings.Contains("LinksVerticalSeparator")) Then
                For intCounter As Integer = 0 To ListOfLinks.Count - 1
                    strRenderedHTML += CreateSiteLink(ListOfLinks(intCounter))
                    If Not (intCounter) = (ListOfLinks.Count - 1) Then
                        strRenderedHTML += HttpUtility.HtmlDecode(Settings("LinksVerticalSeparator"))
                    End If
                Next
            Else
                For intCounter As Integer = 0 To ListOfLinks.Count - 1
                    strRenderedHTML += CreateSiteLink(ListOfLinks(intCounter))
                    If Not (intCounter) = (ListOfLinks.Count - 1) Then
                        strRenderedHTML += "<br/>"
                    End If
                Next
            End If
            strRenderedHTML += "</div>"
            Dim PlainHTMLControl As New HtmlControls.HtmlGenericControl
            PlainHTMLControl.InnerHtml = strRenderedHTML
            pnlSiteLinksRenderArea.Controls.Add(PlainHTMLControl)
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' RenderHorizontalListOfLinks renders a horizontal list of links and adds these links to
    ''' the "SiteLinksRenderArea" panel on the view control.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub RenderHorizontalListOfLinks(ByRef ListOfLinks As Classes.SitePages)
        Try
            Dim strRenderedHTML As String = ""
            If (Settings.Contains("LinksCSSClass")) Then
                strRenderedHTML += "<div class='" + HttpUtility.HtmlDecode(Settings("LinksCSSClass")) + "'>"
            Else
                strRenderedHTML += "<div>"
            End If
            If (Settings.Contains("LinksHorizontalSeparator")) Then
                For intCounter As Integer = 0 To ListOfLinks.Count - 1
                    strRenderedHTML += CreateSiteLink(ListOfLinks(intCounter))
                    If Not (intCounter) = (ListOfLinks.Count - 1) Then
                        strRenderedHTML += HttpUtility.HtmlDecode(Settings("LinksHorizontalSeparator"))
                    End If
                Next
            Else
                For intCounter As Integer = 0 To ListOfLinks.Count - 1
                    strRenderedHTML += CreateSiteLink(ListOfLinks(intCounter))
                    If Not (intCounter) = (ListOfLinks.Count - 1) Then
                        strRenderedHTML += "&nbsp;"
                    End If
                Next
            End If
            strRenderedHTML += "</div>"
            Dim PlainHTMLControl As New HtmlControls.HtmlGenericControl
            PlainHTMLControl.InnerHtml = strRenderedHTML
            pnlSiteLinksRenderArea.Controls.Add(PlainHTMLControl)
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Page_Load runs when the control is loaded
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            If (Settings.Contains("LinksUseCustomCSS")) Then
                Dim CSSStylesHeader As New HtmlControls.HtmlGenericControl
                CSSStylesHeader.InnerHtml += "<style type='text/css'>"
                If (Settings.Contains("LinksCustomCSS")) Then
                    CSSStylesHeader.InnerHtml += HttpUtility.HtmlDecode(Settings("LinksCustomCSS"))
                End If
                CSSStylesHeader.InnerHtml += "</style>"
                pnlSiteLinksRenderArea.Controls.Add(CSSStylesHeader)
            End If
            If (Settings.Contains("LinksHeader")) Then
                Dim PlainHTMLHeader As New HtmlControls.HtmlGenericControl
                PlainHTMLHeader.InnerHtml = HttpUtility.HtmlDecode(Settings("LinksHeader"))
                pnlSiteLinksRenderArea.Controls.Add(PlainHTMLHeader)
            End If
            Dim SiteDatabase As New SiteLinksController
            Dim ListOfPages As Classes.SitePages
            Dim RootTabID As Integer
            If (Settings.Contains("LinksRootPage")) Then
                RootTabID = CInt(Settings("LinksRootPage"))
                If RootTabID = -1 Then
                    RootTabID = TabId
                Else
                End If
            Else
                RootTabID = TabId
            End If
            If (Settings.Contains("LinksType")) Then
                Dim CurrentLinksType As Classes.LinksType = CInt(Settings("LinksType"))
                Select Case CurrentLinksType
                    Case Classes.LinksType.SameLevel
                        ListOfPages = SiteDatabase.GetSameLevelPages(RootTabID)
                    Case Classes.LinksType.ParentLevel
                        ListOfPages = SiteDatabase.GetParentPages(RootTabID)
                    Case Classes.LinksType.ChildLevel
                        ListOfPages = SiteDatabase.GetChildPages(RootTabID)
                    Case Classes.LinksType.SiteRoot
                        ListOfPages = SiteDatabase.GetRootPages
                    Case Else
                        ListOfPages = SiteDatabase.GetSameLevelPages(RootTabID)
                End Select
            Else
                ListOfPages = SiteDatabase.GetSameLevelPages(RootTabID)
            End If
            pnlSiteLinksRenderArea.Controls.Clear()
            If (Settings.Contains("LinksHeader")) Then
                Dim PlainHTMLHeader As New HtmlControls.HtmlGenericControl
                PlainHTMLHeader.InnerHtml = HttpUtility.HtmlDecode(Settings("LinksHeader"))
                pnlSiteLinksRenderArea.Controls.Add(PlainHTMLHeader)
            End If
            If (Settings.Contains("LinksStyle")) Then
                Dim CurrentLinksStyle As Classes.LinksStyle = CInt(Settings("LinksStyle"))
                Select Case CurrentLinksStyle
                    Case Classes.LinksStyle.VerticalList
                        RenderVerticalListOfLinks(ListOfPages)
                    Case Classes.LinksStyle.HorizontalList
                        RenderHorizontalListOfLinks(ListOfPages)
                    Case Classes.LinksStyle.OrderedList
                        RenderOrderedListOfLinks(ListOfPages)
                    Case Classes.LinksStyle.UnorderedList
                        RenderUnorderedListOfLinks(ListOfPages)
                End Select
            Else
                RenderVerticalListOfLinks(ListOfPages)
            End If
            If (Settings.Contains("LinksFooter")) Then
                Dim PlainHTMLHeader As New HtmlControls.HtmlGenericControl
                PlainHTMLHeader.InnerHtml = HttpUtility.HtmlDecode(Settings("LinksFooter"))
                pnlSiteLinksRenderArea.Controls.Add(PlainHTMLHeader)
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try

    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Registers the module actions required for interfacing with the portal framework
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions
        Get
            Dim Actions As New ModuleActionCollection
            'Actions.Add(GetNextActionID, Localization.GetString("EditModule", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
            Return Actions
        End Get
    End Property

End Class